<?php

/*
 * 验证用户名 4-12英文或者字母
 */

function verify_username($username) {
    return ctype_alnum($username) && strlen($username) >= 4 && strlen($username) <= 12 ? true : false;
}

/*
 * 验证密码 6-12英文,字母或下划线
 */

function verify_password($password) {
    $preg = '/^[A-Za-z\d_]{6,12}$/';
    return preg_match($preg, $password) ? true : false;
}

/*
 * 验证邮箱格式
 */

function verify_email($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL) ? true : false;
}

/*
 * 获取用户ip
 */

function getIp() {
    return $_SERVER['REMOTE_ADDR'];
}

/*
 * 获取当前时间
 */

function tnow() {
    return date('Y-m-d H:i:s');
}

/*
 * 添加干扰字符
 */

//function add_disturb($str){
//    $disturbed_str = [];
//    $upper = range('A', 'Z');
//    $lower = range('a', 'z');
//    $symbol = ['-','/','~','!','#','$','%','^','&','*','(',')'];
//    $disturb_arr = array_merge($upper,$lower,$symbol);
//    $str_arr = preg_split('/(?<!^)(?!$)/u', $str );
//    foreach($str_arr as $k=>$v){
//        $index = mt_rand(0, 63);
//        $disturbed_str[$k*2]=$v;
//        $disturbed_str[$k*2+1] = $disturb_arr[$index];
//    }
//    return implode('',$disturbed_str);
//}

/*
 * 添加干扰,新算法
 */

//function add_disturb($str) {
//    $shuffledstr = '';//打乱后的字符串
//    if (strpos($str, '|')) {
//        $strArr = explode('|', $str);
//        $shuffle_part = $strArr[0];//需要打乱的字符部分
//        $immune_part = $strArr[1];//免疫字符部门
//    } else {
//        $shuffle_part = $str;
//        $immune_part = '';
//    }
//    $shuffle_part_arr = preg_split('/(?<!^)(?!$)/u', $shuffle_part);
//    $shuffle_part_arr = array_chunk($shuffle_part_arr, 2);
//    $shuffled_part_str = '';
//    foreach ($shuffle_part_arr as $k => $rows) {
//        shuffle($rows);
//        $shuffled_part_str .= implode('', $rows);
//    }
//    $shuffledstr = $shuffled_part_str . $immune_part;
//    return $shuffledstr;
//}

/*
 * 添加干扰
 */
function add_disturb($str, $delimter = '|') {
    $shuffledstr = '';
    $strarr = explode($delimter, $str);
    foreach ($strarr as $k => $v) {
        if (($k + 1) % 2 == 0) {
            $shuffled_part_str = '';
            $shuffle_part_arr = array_chunk(preg_split('/(?<!^)(?!$)/u', $v), 2);
            foreach ($shuffle_part_arr as $vs) {
                shuffle($vs);
                $shuffled_part_str .= implode('', $vs);
            }
            $v = $shuffled_part_str;
        }
        $shuffledstr[$k] = $v;
    }
    return implode('', $shuffledstr);
}






/*
 * 请求接口
 */

function CURLSend($url, $method = 'get', $data = '') {
    $ch = curl_init(); //初始化
    $headers = array('Accept-Charset: utf-8');
    //设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //指定请求的URL
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); //提交方式
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //不验证SSL
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //不验证SSL
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置HTTP头字段的数组
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE 5.01;Windows NT 5.0)'); //头的字符串

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); //自动设置header中的Referer:信息

    curl_setopt($ch, CURLOPT_POSTFIELDS, ['jsonstr' => json_encode($data)]);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //是否输出到屏幕上,true不直接输出
    $temp = curl_exec($ch); //执行并获取结果
    curl_close($ch);
    return $temp; //return 返回值json字符串
}

/*
 * 生成验证码
 */

function create_verify_code() {
    return mt_rand(1000, 9999);
}
